﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace UniCT.LinkShareWpf.Interface
{
    [DataContract]
    [Serializable]
    [XmlRoot("Resource")]
    public class Resource : INotifyPropertyChanged, IDataErrorInfo
    {
        
        [DataMember]
        TechnicalInfo tInfo;
        [DataMember]
        ContentInfo cInfo;
        [DataMember]
        double rating;
        [DataMember]
        String url;
      
        public Resource()
        {
            tInfo = new TechnicalInfo();
            cInfo = new ContentInfo();
            rating = 0;
            url = "Ciao";
          
        }


        public Resource(Resource res)
        {
            this.tInfo = res.TInfo;
            this.cInfo = res.CInfo;
            this.rating = res.Rating;
            this.url = res.Url;

        }

        public Resource(NetResource netRes)
        {
            this.tInfo = netRes.TInfo;
            this.cInfo = netRes.CInfo;
            this.rating = netRes.rating;
            this.url = netRes.Url;
        }

        public Resource(TechnicalInfo ti, ContentInfoTVS ci)
        {
            tInfo = ti;
            cInfo = ci;
            rating = 0;
         

        }

        public Resource(TechnicalInfo ti, ContentInfo ci)
        {
            tInfo = ti;
            cInfo = ci ;
            rating = 0;
          

        }

        public ContentInfo CInfo
        {
            get { return this.cInfo; }
            set
            {
                if (value != this.cInfo)
                {
                    this.cInfo = value;
                    this.OnPropertyChanged("CInfo");
                }
            }
        }

        public TechnicalInfo TInfo
        {
            get { return this.tInfo; }
            set
            {
                if (value != this.tInfo)
                {
                    this.tInfo = value;
                    this.OnPropertyChanged("TInfo");
                }
            }
        }

        public double Rating
        {
            get { return this.rating; }
            set
            {
                if (value != this.rating)
                {
                    this.rating = value;
                    this.OnPropertyChanged("Rating");
                }
            }
        }

        public String Url
        {
            get { return this.url; }
            set
            {
                if (value != this.url)
                {
                    this.url = value;
                    this.OnPropertyChanged("Url");
               
                }
            }
        }

        public override string ToString()
        {
            return string.Format("{0}, {1}, {2}, {3}", cInfo.ToString(), tInfo.ToString(), url, rating);
        }

        #region INotifyPropertyChanged Support

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string propName]
        {
            get
            {
                if (propName == "Url")
                {
                    if (this.Url.Equals(string.Empty))
                    {
                        return "Non può essere vuoto";
                    }
                }
               


                return null;
            }
        }

        #endregion
    }

}
